﻿// CompositionDlg.cpp: 实现文件
//

#include "pch.h"
#include "Steam.h"
#include "CompositionDlg.h"
#include "afxdialogex.h"
#include "PropertyData.h"

// CCompositionDlg 对话框

IMPLEMENT_DYNAMIC(CCompositionDlg, CDialogEx)

CCompositionDlg::CCompositionDlg(char _components[ncmax][refpropcharlength], double*_x, long _ncomp, CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_COMPOSITION_DIALOG, pParent)
{
	ncomp = _ncomp;
	
	for (int i = 0; i < ncomp; i++)
	{
		for (int j = 0; j < 121; j++)
		{
			if (strcmp(_components[i], componentList[j][1]) == 0)
			{
				strcpy_s(components[i], sizeof components[i] / sizeof components[i][0], componentList[j][0]);
			}
		}
		x[i] = _x[i];
	}
}

CCompositionDlg::~CCompositionDlg()
{
}

void CCompositionDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_X_LIST, m_xListEdit);
}


BEGIN_MESSAGE_MAP(CCompositionDlg, CDialogEx)
	ON_BN_CLICKED(IDOK, &CCompositionDlg::OnBnClickedOk)
END_MESSAGE_MAP()


// CCompositionDlg 消息处理程序


BOOL CCompositionDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// TODO:  在此添加额外的初始化
	CRect rect;
	m_xListEdit.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES | LVS_EX_ONECLICKACTIVATE);
	m_xListEdit.GetClientRect(rect);
	m_xListEdit.InsertColumn(0, _T("组分"), LVCFMT_RIGHT, rect.Width() / 2, 0);
	m_xListEdit.InsertColumn(1, _T("摩尔分数"), LVCFMT_CENTER, rect.Width() / 2, 1);
	
	//这个地方特别注意，CListCtrl中第1列无法直接设置对齐样式，需要用以下方法
	LVCOLUMN lvc;
	lvc.mask = LVCF_FMT;
	m_xListEdit.GetColumn(0, &lvc);
	lvc.fmt &= ~LVCFMT_JUSTIFYMASK;
	lvc.fmt |= LVCFMT_CENTER;
	m_xListEdit.SetColumn(0, &lvc);
	for (size_t i = 0; i < ncomp; i++)
	{
		m_xListEdit.InsertItem(i, CA2T(components[i]));
		CString strX;
		strX.Format(_T("%g"), x[i]);
		m_xListEdit.SetItemText(i, 1, strX);
	}

	m_xListEdit.EnableTips(TRUE);
	m_xListEdit.ReadOnlyCol(0);

	return TRUE;  // return TRUE unless you set the focus to a control
				  // 异常: OCX 属性页应返回 FALSE
}


void CCompositionDlg::OnBnClickedOk()
{
	// TODO: 在此添加控件通知处理程序代码
	//CString str;
	//str = m_xListEdit.GetItemText(1, 1);
	double sum = 0;
	for (size_t i = 0; i < ncomp; i++)
	{
		x[i] = _ttof(m_xListEdit.GetItemText(i, 1));
		sum += x[i];
	}

	if (sum < 0.99)
	{
		AfxMessageBox(L"摩尔比和小于0.99");
		return;
	}
	else if (sum > 1.01 && sum < 99)
	{
		AfxMessageBox(L"摩尔比和不正确");
		return;
	}
	else if (sum > 101)
	{
		AfxMessageBox(L"摩尔比和大于101");
		return;
	}
	if (sum > 99)
	{
		for (size_t i = 0; i < ncomp; i++)
		{
			x[i] = x[i] / 100;
		}
	}
	CDialogEx::OnOK();
}
